//
// Prob.cpp for 207correl in /home/druart_p/Dropbox/Work/Teck2/math/my-math-epi/207
//
// Made by pierrick druart
// Login   <druart_p@epitech.net>
//
// Started on  Wed May 16 11:30:14 2012 pierrick druart
// Last update Thu May 24 09:12:34 2012 pierrick druart
//

#include "Prob.hpp"
#include "Calc.hh"

// CONSTRUCTOR & DESTRUCTOR

Prob::Prob(int & ac, char **av)
{
  std::istringstream	iss(av[1]);

  iss >> this->val_;
  this->ac = ac;
  this->av = av;
  if (ac > 4)
    throw my_exception_help();
  getValueFromFile(av[2]);
  if (this->val_ <= 2 && this->val_ > 0)
    {
      this->func_[1] = &Prob::option1;
      this->func_[2] = &Prob::option2;
    }
  else
    throw my_exception_arg(av[1]);
  exec();
}

Prob::~Prob()
{
  this->func_.clear();
}

// EXECUTION

void	Prob::exec()
{
  (this->*func_[this->val_])();
}

// DATA FILE

void	Prob::getValueFromFile(const std::string & file)
{
  std::string	line;
  const char	*test = file.c_str();
  std::ifstream myfile(test);

  if (myfile.is_open())
    {
      while (myfile.good())
	{
	  int		boy = 0;
	  int		girl = 0;
	  getline(myfile, line);
	  std::stringstream	tmp(line);
	  tmp >> girl;
	  tmp >> boy;
	  if (boy != 0 && girl != 0)
	    {
	      this->_boy.push_back(boy);
	      this->_girl.push_back(girl);
	    }
	}
      myfile.close();
    }
}

// OPTION

void	Prob::option1()
{
  Calc		coef;

  coef.coefx_to_y(this->_boy, this->_girl);
  coef.coefy_to_x(this->_boy, this->_girl);
  coef.correl(this->_boy, this->_girl);
}

void	Prob::option2()
{
  QApplication		app(this->ac, this->av);
  Surface	       	surface(this->av, this->_girl, this->_boy);

  surface.show();
  app.exec();
}

// CHECK ARG

void	Prob::verif_arg(char **av)
{
  for (int i = 2;av[i]; i++)
    {
      for (int j = 0;av[i][j]; j++)
	{
	  if (av[i][j] < '0' || av[i][j] > '9')
	    if (av[i][j] != '.')
	      throw my_exception_arg(av[i]);
	}
    }

}
